package site.assad.dao;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import site.assad.domain.User;

import java.util.List;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/31 19:23
 * Description: User 的相关 DAO 对象，通过注解方式配置
 */
public interface UserDaoAnno {

    //相当于 <select> 标签
    @Select({ "SELECT user_id,user_name,user_password,create_date FROM users"})
    //相当于 <resultMap> 标签
    @Results(id = "userAnnoMap", value = {
            @Result(property = "id", column = "user_id", id = true),
            @Result(property = "name", column = "user_name"),
            @Result(property = "password", column = "user_password"),
            @Result(property = "createDate", column = "create_date", jdbcType = JdbcType.DATE, javaType = java.util.Date.class),
    })
    List<User> getAllUser();

    @Select({"SELECT user_id,user_name,user_password,create_date FROM users " +
            "WHERE user_id = #{id}"})
    @ResultMap("userAnnoMap")
    User getUserById(int id);

    //相当于 <insert> 标签
    @Insert("INSERT INTO users(user_name,user_password,create_date) " +
            "VALUES(#{name},#{password},#{createDate, jdbcType=DATE})")
    @ResultMap("userAnnoMap")
    int addUser(User user);

    //相当于 <update> 标签
    @Update("UPDATE users " +
            "SET user_name = #{name}, user_password = #{password} " +
            "WHERE user_id = #{id}")
    @ResultMap("userAnnoMap")
    int updateUserById(User user);

    //相当于 <delete> 标签
    @Delete("DELETE FROM users " +
            "WHERE user_id = #{id}")
    @ResultMap("userAnnoMap")
    int removeUserById(int id);

}
